<?php

class Books extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->model('books_model');
    }
    function index(){
        $data['page_title'] = "Book Index";
        $data['all_books'] = $this->books_model->getAll();                
        $this->load->view('books/index', $data);
    }
    function newBook(){
        
        if ($this->input->post('title')){
            $bookInfo = array(
                'title' => $this->input->post('title'),
                'isbn' => $this->input->post('isbn')
            );
            $this->books_model->insertNewBook($bookInfo);
            redirect('books');
        }
        else{
            $data['page_title'] = "New Book";
            $this->load->view('books/newBook', $data);
        }
    }
    
    function editBook($id){
        if ($this->input->post('id')){
            $bookInfo = array(
                'id' => $this->input->post('id'),
                'title' => $this->input->post('title'),
                'isbn' => $this->input->post('isbn')
            );
            $this->books_model->updateBook($bookInfo);
            redirect('books');
        }
        else{
            $data['page_title'] = "Edit Book";
            $data['book'] = $this->books_model->getById($id);
            $this->load->view('books/editBook', $data);
        }
    }
    function deleteBook($id){
        $this->books_model->deleteBook($id);
        redirect('books');
    }
}